﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EzDesk.DesktopClient.ServiceLocator.ClientService;
using System.IO;
using System.Xml.Linq;

namespace EzDesk.DesktopClient.ServiceLocator
{
    class StubCoreClientService : ICoreClientService
    {
        public string XmlFeed = "EzDeskFeed.xml";
        public string BlankFeed = "BlankFeedInformation.xml";
        public string OutputStr(string str)
        {
            return "Stringsssss from server: " + str;
        }

        public App[] GetCurrentApps()
        {
            App app = new App { Name = "attendence", Title = "休假", Domain = "ddd" };
            return new App[] { app
                ,
                new App{ Name="project", Title="项目", Domain="superwolf"}
            };
        }

        public string GetXmlUpdateFeedBetweenDate(Guid userId, DateTime from, DateTime to, bool? isRead, bool? isAvaiable)
        {
            return GenerateFeedStringWithDifferentGuidAndDifferntUpdateDate();
        }

        public string GetXmlNotificationBetweenDate(Guid userId, DateTime from, DateTime to, bool? isRead, bool? isAvaiable)
        {
            return GenerateFeedStringWithDifferentGuidAndDifferntUpdateDate();
        }
        private string GenerateFeedStringWithDifferentDate()
        {
            StringBuilder sb = new StringBuilder(File.ReadAllText(XmlFeed));
            sb.Replace("2010-3-20", DateTime.Now.ToString());
            sb.Replace("2010-3-21", DateTime.Now.AddMilliseconds(1).ToString());
            sb.Replace("2010-3-22", DateTime.Now.AddMilliseconds(2).ToString());
            return sb.ToString();

        }

        private string GenerateFeedStringWithDifferentGuidAndDifferntUpdateDate()
        {
            StringBuilder sb = new StringBuilder(File.ReadAllText(XmlFeed));
            XElement xelement = XElement.Parse(sb.ToString());
            int count = 0;
            foreach (var item in xelement.Descendants("feeditem"))
            {
                item.SetAttributeValue("guid", Guid.NewGuid());
                item.SetAttributeValue("updateon", DateTime.Now.AddSeconds(count));
                item.SetAttributeValue("createon", DateTime.Now.AddSeconds(count));
                item.SetAttributeValue("lastchangeon", DateTime.Now.AddSeconds(count));

                item.SetAttributeValue("isavailable", true);
                count++;
            }
            return xelement.ToString();
        }

        public string GetXmlUpdateFeedByCount(Guid userId, int startIndex, int count, bool? isRead, bool? isAvaiable)
        {
            throw new NotImplementedException();
        }

        public string GetXmlNotificationByCount(Guid userId, int startIndex, int count, bool? isRead, bool? isAvaiable)
        {
            throw new NotImplementedException();
        }

        public void SetFeedIsRead(Guid feedId, bool isRead)
        {
            return;
        }

        public void ChangeUserDept(Guid userId, Guid deptId)
        {
            throw new NotImplementedException();
        }

        public void ChangeUserReportId(Guid userId, Guid reportId)
        {
            throw new NotImplementedException();
        }

        public void ChangeUserPosition(Guid userId, Guid positionId)
        {
            throw new NotImplementedException();
        }

        public UserProfile CreateEmptyUserProfile(string userName)
        {
            throw new NotImplementedException();
        }

        public UserProfile GetUserProfileById(Guid userId)
        {
            throw new NotImplementedException();
        }

        public SimpleUser GetSimpleUserById(Guid userId)
        {
            throw new NotImplementedException();
        }

        public SimpleUser[] GetSimpleUserInfos(string userIdStrs)
        {
            throw new NotImplementedException();
        }

        public UserProfile Login(string uid, string pwd)
        {
            UserProfile user = new UserProfile()
            {
                Id = Guid.NewGuid(),
                Name = "黄冠",
                UserName = "青山老妖"
            };
            return user;
        }

        #region ICoreClientService Members


        public string GetXmlFeedsBetweenDate(string feedTypeStr, Guid userId, DateTime from, DateTime to, bool? isRead, bool? isAvaiable)
        {
            return GenerateFeedStringWithDifferentGuidAndDifferntUpdateDate();
        }

        public string GetXmlFeedsByCount(string feedTypeStr, Guid userId, int startIndex, int count, bool? isRead, bool? isAvaiable)
        {
            throw new NotImplementedException();
        }

        public void SetFeedIsRead(Guid feedId, string memberString, bool isRead)
        {
            throw new NotImplementedException();
        }

        public void SetFeedsIsRead(string feedIdStrs, string memberString, bool isRead)
        {
            return;
        }

        #endregion

        #region ICoreClientService Members


        public App GetAppByName(string appName)
        {
            throw new NotImplementedException();
        }

        public AppTask[] GetAllAppTasks(Guid userId, string userAgent)
        {
            if (userAgent != "DesktopClient") return null;
            var tasks = GetAppTasksByAppName("attendence", Guid.NewGuid(), userAgent);
            return tasks.Union(GetAppTasksByAppName("project", Guid.NewGuid(), userAgent)).ToArray();
        }

        public AppTask[] GetAppTasksByAppName(string appName, Guid userId, string userAgent)
        {
            if (userAgent != "DesktopClient") return null;

            return new AppTask[] { 
                new AppTask{ AppName=appName, Description="app1", Title="App1" , FuncUrl="http://www.google.com"}
                , new AppTask{ AppName=appName, Description="app1", Title="App1" , FuncUrl="http://www.google.com"}
                , new AppTask{ AppName=appName, Description="app1", Title="App1" , FuncUrl="http://www.google.com"}
                    , new AppTask{ AppName=appName, Description="app1", Title="App1" , FuncUrl="http://www.google.com"}
                , new AppTask{ AppName=appName, Description="app1", Title="App1" , FuncUrl="http://www.google.com"}
                    , new AppTask{ AppName=appName, Description="app1", Title="App1" , FuncUrl="http://www.google.com"}
                , new AppTask{ AppName=appName, Description="app1", Title="App1" , FuncUrl="http://www.google.com"}
                    , new AppTask{ AppName=appName, Description="app1", Title="App1" , FuncUrl="http://www.google.com"}
                , new AppTask{ AppName=appName, Description="app1", Title="App1" , FuncUrl="http://www.google.com"}
            };
        }

        string ICoreClientService.Login(string uid, string pwd)
        {
            if (string.IsNullOrEmpty(uid))
            {
                return null;
            }
            return "superwolf";
        }

        public UserProfile GetUserProfileByUserName(string userName)
        {
            UserProfile user = new UserProfile()
            {
                Id = Guid.NewGuid(),
                Name = "黄冠",
                UserName = "青山老妖"
            };
            return user;
        }

        #endregion
    }
}
